﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FFS.Model;
using FFS.BLL;

namespace FFS
{
    public partial class frmSysRemind : Form
    {
        public frmSysRemind()
        {
            InitializeComponent();
        }

        public string actionType = "";
        SysRemindInfo remind = new SysRemindInfo();
        List<SysRemindInfo> remindList = new List<SysRemindInfo>();
        SysRemindService remindService = new SysRemindService();

        private void BindDataGridView(List<SysRemindInfo> remindList)
        {
            this.dgv_SysRemindList.Rows.Clear();
            foreach (SysRemindInfo remind in remindList)
            {
                string IsActiveName = remind.IsActive.ToString();
                if (remind.IsActive)
                {
                    IsActiveName = "是";
                }
                else
                {
                    IsActiveName = "否";
                }
                string IsRemindedName = remind.IsReminded.ToString();
                if (remind.IsReminded)
                {
                    IsRemindedName = "是";
                }
                else
                {
                    IsRemindedName = "否";
                }
                this.dgv_SysRemindList.Rows.Add(remind.SysRemindID, remind.RemindType, remind.RemindTitle, remind.RemindContent, remind.RemindStartTime, remind.RemindEndTime, remind.IsActive, IsActiveName, remind.IsReminded,IsRemindedName);
            }
        }

        private void frSysRemind_Load(object sender, EventArgs e)
        {
            this.chk_searchAll.Visible = false;
            this.panel_SysRemindInfo.Visible = false;
            this.cmb_RemindType.SelectedIndex = 0;
            this.nud_SysRemindID.Visible = false;
            this.cmb_RemindType.DropDownStyle = ComboBoxStyle.DropDownList;

            this.dgv_SysRemindList.Columns.Add("SysRemindID","提醒编号");
            this.dgv_SysRemindList.Columns[0].Visible = false;
            this.dgv_SysRemindList.Columns.Add("RemindType","提醒类型");
            this.dgv_SysRemindList.Columns.Add("RemindTitle","提醒标题");
            this.dgv_SysRemindList.Columns.Add("RemindContent","提醒内容");
            this.dgv_SysRemindList.Columns.Add("RemindStartTime","提醒开始时间");
            this.dgv_SysRemindList.Columns.Add("RemindEndTime","提醒结束时间");
            this.dgv_SysRemindList.Columns.Add("IsActive", "是否激活");
            this.dgv_SysRemindList.Columns[6].Visible = false;
            this.dgv_SysRemindList.Columns.Add("IsActiveName", "是否激活");
            this.dgv_SysRemindList.Columns.Add("IsReminded", "是否已提醒");
            this.dgv_SysRemindList.Columns[8].Visible = false;
            this.dgv_SysRemindList.Columns.Add("IsRemindedName", "是否已提醒");

            this.dgv_SysRemindList.Width = this.Width - 317;
            this.dgv_SysRemindList.Height = this.Height - 25;

            if (actionType == "view")
            {
                this.ControlFieldStatus(false);
                this.tsb_add.Visible = false;
                this.tsb_delete.Visible = false;
                this.tsb_edit.Visible = false;
                this.tsb_search.Visible = false;
                this.BindDataGridView(remindService.ManageSysRemind().GetCurrentSysRemind(DateTime.Now));
            }
            else
            {
                this.BindDataGridView(remindService.ManageSysRemind().GetAllSysRemind());
            }
        }

        private void ControlFieldStatus(bool IsEnabled)
        {
            this.cmb_RemindType.Enabled = IsEnabled;
            this.txt_RemindTitle.Enabled = IsEnabled;
            this.txt_RemindContent.Enabled = IsEnabled;
            this.dtp_RemindStartTime.Enabled = IsEnabled;
            this.dtp_RemindEndTime.Enabled = IsEnabled;
            this.chk_IsActive.Enabled = IsEnabled;
            this.chk_IsReminded.Enabled = IsEnabled;
            this.btn_OK.Enabled = IsEnabled;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            string promptInfo = "";
            if (actionType == "Add" || actionType == "Edit")
            {
                if (this.txt_RemindTitle.Text.Trim() == "")
                {
                    promptInfo += "提醒标题不能为空！\n";
                }
                if (this.txt_RemindContent.Text.Trim() == "")
                {
                    promptInfo += "提醒内容不能为空！\n";
                }
                if (this.dtp_RemindStartTime.Text.ToString().Trim() == "")
                {
                    promptInfo += "提醒开始时间不能为空！\n";
                }
                if (this.dtp_RemindEndTime.Text.ToString().Trim() == "")
                {
                    promptInfo += "提醒结束时间不能为空！\n";
                }
                if (this.dtp_RemindStartTime.Text.ToString().Trim() != "" && this.dtp_RemindEndTime.Text.ToString().Trim() != "")
                {
                    if (DateTime.Parse(this.dtp_RemindStartTime.Text) > DateTime.Parse(this.dtp_RemindEndTime.Text))
                    {
                        promptInfo += "提醒开始时间不能大于提醒结束时间！\n";
                    }
                }
                if (promptInfo.Trim() != "")
                {
                    MessageBox.Show(promptInfo);
                }
                else
                {
                    if (this.cmb_RemindType.SelectedIndex == 0)
                    {
                        remind.RemindType = "提醒一次";
                    }
                    else
                    {
                        remind.RemindType = "提醒多次";
                    }
                    remind.SysRemindID = (int)this.nud_SysRemindID.Value;
                    remind.RemindTitle = this.txt_RemindTitle.Text.Trim();
                    remind.RemindContent = this.txt_RemindContent.Text;
                    remind.RemindStartTime = DateTime.Parse(this.dtp_RemindStartTime.Text);
                    remind.RemindEndTime = DateTime.Parse(this.dtp_RemindEndTime.Text);
                    remind.IsReminded = this.chk_IsReminded.Checked;
                    remind.IsActive = this.chk_IsActive.Checked;
                    remind.Input_User = Context._Login_UserName;
                    if (actionType == "Add")
                    {
                        remind.SysRemindID = remindService.ManageSysRemind().CreateSysRemind(remind);
                        MessageBox.Show("添加提醒成功");
                    }
                    else 
                    {
                        remindService.ManageSysRemind().ModifySysRemind(remind);
                        MessageBox.Show("编辑提醒成功");
                    }
                    this.BindDataGridView(remindService.ManageSysRemind().GetSysRemindByRemindID(remind.SysRemindID));
                    this.panel_SysRemindInfo.Visible = false;
                }
            }
            else if (actionType == "delete")
            {
                remind.SysRemindID = (int)this.nud_SysRemindID.Value;
                remind.Input_User = Context._Login_UserName;
                remindService.ManageSysRemind().DeleteSysRemind(remind);
                MessageBox.Show("删除成功");
                this.BindDataGridView(remindService.ManageSysRemind().GetAllSysRemind());
            }
            else if (actionType == "search")
            {
                string searchContent = " 1=1 ";
                if (!this.chk_searchAll.Checked)
                {
                    if (this.cmb_RemindType.SelectedIndex == 0)
                    {
                        searchContent += " and RemindType = N'提醒一次' ";
                    }
                    else
                    {
                        searchContent += " and RemindType = N'提醒多次' ";
                    }
                    if (this.txt_RemindTitle.Text.Trim() != "")
                    {
                        searchContent += " and RemindTitle like N'%"+ this.txt_RemindTitle.Text +"%' ";
                    }
                    if (this.txt_RemindContent.Text.Trim() != "")
                    {
                        searchContent += " and RemindContent like N'%" + this.txt_RemindContent.Text + "%' ";
                    }
                    searchContent += " and IsReminded  = '" + this.chk_IsReminded.Checked.ToString().Trim()+"'";
                    searchContent += " and IsActive  = '" + this.chk_IsActive.Checked.ToString().Trim() + "'";
                }

                this.BindDataGridView(remindService.ManageSysRemind().GetSysRemindBySearchContent(searchContent));
            }
            else
            {
                MessageBox.Show("当前操作无效");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.panel_SysRemindInfo.Visible = false;
            this.tsb_add.Enabled = true;
            this.tsb_edit.Enabled = true;
            this.tsb_delete.Enabled = true;
            this.tsb_search.Enabled = true;
        }

        private void tsb_add_Click(object sender, EventArgs e)
        {
            this.actionType = "Add";
            this.cmb_RemindType.SelectedIndex = 0;
            this.txt_RemindTitle.Text = "";
            this.txt_RemindContent.Text = "";
            this.chk_IsActive.Checked = false;
            this.chk_IsReminded.Checked = false;
            this.dtp_RemindStartTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
            this.dtp_RemindEndTime.Text = "";
            this.chk_searchAll.Checked = false;
            this.ControlFieldStatus(true);
            this.btn_OK.Text = "保  存";
            this.tsb_edit.Enabled = false;
            this.tsb_delete.Enabled = false;
            this.tsb_search.Enabled = false;
            this.panel_SysRemindInfo.Visible = true;
        }

        private void tsb_edit_Click(object sender, EventArgs e)
        {
            if (this.dgv_SysRemindList.SelectedRows.Count == 1)
            {
                this.actionType = "Edit";
                this.chk_searchAll.Checked = false;
                this.ControlFieldStatus(true);
                this.btn_OK.Text = "保  存";
                this.tsb_add.Enabled = false;
                this.tsb_delete.Enabled = false;
                this.tsb_search.Enabled = false;
                this.panel_SysRemindInfo.Visible = true;
            }
            else
            {
                MessageBox.Show("请选择编辑行");
            }
        }

        private void tsb_delete_Click(object sender, EventArgs e)
        {

            if (this.dgv_SysRemindList.SelectedRows.Count == 1)
            {
                this.actionType = "delete";
                this.ControlFieldStatus(false);
                this.chk_searchAll.Checked = false;
                this.btn_OK.Enabled = true;
                this.btn_OK.Text = "删  除";
                this.tsb_edit.Enabled = false;
                this.tsb_add.Enabled = false;
                this.tsb_search.Enabled = false;
                this.panel_SysRemindInfo.Visible = true;
            }
            else
            {
                MessageBox.Show("请选择删除行");
            }

        }

        private void tsb_search_Click(object sender, EventArgs e)
        {
            this.actionType = "search";
            this.chk_searchAll.Visible = true;
            this.chk_searchAll.Checked = false;
            this.ControlFieldStatus(true);
            this.btn_OK.Text = "查  询";
            this.tsb_edit.Enabled = false;
            this.tsb_delete.Enabled = false;
            this.tsb_add.Enabled = false;
            this.panel_SysRemindInfo.Visible = true;
        }

        private void chk_searchAll_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chk_searchAll.Checked)
            {
                this.ControlFieldStatus(false);
                this.btn_OK.Enabled = true;
                this.btn_OK.Text = "显示全部";
            }
            else
            {
                this.ControlFieldStatus(true);
                this.btn_OK.Enabled = true;
                this.btn_OK.Text = "查  询";
            }
        }

        private void dgv_SysRemindList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgv_SysRemindList.SelectedRows.Count == 1)
                {
                    this.nud_SysRemindID.Value = decimal.Parse(this.dgv_SysRemindList.SelectedRows[0].Cells[0].Value.ToString());
                    if (this.dgv_SysRemindList.SelectedRows[0].Cells[1].Value.ToString() == "提醒一次")
                    {
                        this.cmb_RemindType.SelectedIndex = 0;
                    }
                    else
                    {
                        this.cmb_RemindType.SelectedIndex = 1;
                    }
                    this.txt_RemindTitle.Text = this.dgv_SysRemindList.SelectedRows[0].Cells[2].Value.ToString();
                    this.txt_RemindContent.Text = this.dgv_SysRemindList.SelectedRows[0].Cells[3].Value.ToString();
                    this.dtp_RemindStartTime.Text = this.dgv_SysRemindList.SelectedRows[0].Cells[4].Value.ToString();
                    this.dtp_RemindEndTime.Text = this.dgv_SysRemindList.SelectedRows[0].Cells[5].Value.ToString();
                    this.chk_IsActive.Checked = bool.Parse(this.dgv_SysRemindList.SelectedRows[0].Cells[6].Value.ToString());
                    this.chk_IsReminded.Checked = bool.Parse(this.dgv_SysRemindList.SelectedRows[0].Cells[8].Value.ToString());
                }
            }
            catch (Exception)
            {
            }
        }

        private void tsb_Reminded_Click(object sender, EventArgs e)
        {
            if (this.dgv_SysRemindList.SelectedRows.Count == 1)
            {
                if (MessageBox.Show(this.dgv_SysRemindList.SelectedRows[0].Cells[3].Value.ToString(), this.dgv_SysRemindList.SelectedRows[0].Cells[2].Value.ToString(), MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    remindService.ManageSysRemind().UpdateSysRemindStatus(int.Parse(this.dgv_SysRemindList.SelectedRows[0].Cells[0].Value.ToString()), true, Context._Login_UserName);
                    this.BindDataGridView(remindService.ManageSysRemind().GetCurrentSysRemind(DateTime.Now));
                }
            }
            else
            {
                MessageBox.Show("请选择提醒信息");
            }
        }
    }
}
